<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css">
<link rel="stylesheet" href="static/css/style.css" />
<link rel="stylesheet" href="static/css/tcommon.css" />
<script src="static/jquery/jquery-1.12.4.js"></script>
<script src="static/easyui/jquery.easyui.min.js"></script>
<script src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>预算明细管理</title>
</head>

<body>
	<div class="content-container" data-options="region:'center'">
		<div class="panel-header">
			<div class="panel-title">预算列表</div>
		</div>
		<table id="pro_table" class="easyui-datagrid" style="height: 100%">
		</table>
		<div id="pro_tb">
			<div class="search-input">
				项目：
				<input style="width: 200px;" id="projectitemid" name="projectitemid" class="easyui-combobox"
					editable="false"
					data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: 'projectitemlist.do?status=2',
							    onLoadSuccess : function(data){ 
								   	if(data.length>0){
								   		$('#projectitemid').combobox('setValue',data[0].id);
							    	}
							    },onSelect: function(rec){
						    		projectitemid = rec.id;
							    	if(rec.projectitemchilds.length>0){
										var url = 'projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchildid').combobox('reload', url);
										document.getElementById('div1').style.display = '';
							    	}else{
							    		projectitemchildid = 0;
										$('#projectitemchildid').combobox('clear');
										document.getElementById('div1').style.display = 'none';
							    		loadData();
							    	}
							    },panelHeight: 'auto'">
			</div>
			<div id="div1" style="display: none;" class="search-input">
				子项目：
				<input style="width: 200px;" id="projectitemchildid" name="projectitemchildid" class="easyui-combobox"
					editable="false"
					data-options="
							    valueField: 'id',
							    textField: 'name',
							    loadFilter: function (data) {
							        var obj = {};
							        obj.id = '';
							        obj.name = '全部';
							        data.splice(0, 0, obj);
							        return data;
							    },
							    onSelect: function(rec){
							    	projectitemchildid = rec.id;
								    	loadData();
							    },panelHeight: 'auto'">
			</div>
			<div class="search-input">
				状态：
				<select id="status" class="easyui-combobox" name="status" style="width: 120px;" editable="false"
					data-options="
							    onSelect: function(rec){
							   		status=rec.value;
							   		if(isnew){
							   			isnew = false;
							   		}else{
							   			loadData();
							   		}
							   		
							    },panelHeight: 'auto'">
					<option value="">全部</option>
					<option value="3">已拒绝</option>
					<option value="4">已审</option>
					<option value="6">待二级审核</option>
					<option value="7">待一级审核</option>
				</select>
			</div>
			<div>
				<a href="javascript:void(0)" class="d-btn btn-blue" id="add" onclick="newpro()">添加</a>
				<a href="javascript:void(0)" class="d-btn btn-green" id="upt" onclick="updatepro()">编辑</a>
				<a href="javascript:void(0)" class="d-btn btn-red" id="del" onclick="deletepro()">删除</a>
			</div>

		</div>
	</div>
	<div id="proDlg" class="easyui-dialog info_dialog" buttons="#pro-dlg-buttons"
		style="width: 800px; padding: 10px 20px; overflow-y: auto; top: 30px;" closed="true">
		<div class="title"></div>
		<form id="proFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">项目</td>
						<td>
							<input style="width: 200px;" id="projectitem" name="projectitem" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							   	required:true,
							    url: 'projectitemlist.do?status=2',
							    onSelect: function(rec){
								    if(rec.projectitemchilds.length>0){
										document.getElementById('tr1').style.display = '';
										document.getElementById('tr2').style.display = '';
										var url = 'projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchild').combobox('reload', url);
								    }else{
										document.getElementById('tr1').style.display = 'none';
										document.getElementById('tr2').style.display = 'none';
										findbudgetmain(rec.id);
							    	}
							    },panelHeight: 'auto'">
						</td>
						<td id="tr1" style="display: none;">子项目</td>
						<td id="tr2" style="display: none;">
							<input style="width: 200px;" id="projectitemchild" name="projectitemchild" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							    onSelect: function(rec){
									findbudgetmainchild(rec.id);
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">预算类型：</td>
						<td>
							<input style="width: 200px;" id="budgetitem" name="budgetitem" class="easyui-combobox"
								data-options="
							    valueField: 'id',
							    textField: 'content',
							   	required:true,
							    url: 'budgetItemlist.do',
							    onSelect: function(rec){
							   		
							    },panelHeight: 'auto'">
						</td>
						<td style="width: 100px;">金额：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true,validType:'number'" id="money" name="money"
								style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">描述：</td>
						<td colspan="3">
							<input class="easyui-textbox" id="content" name="content" data-options="multiline:true"
								style="width: 550px; height: 80px;" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons">
		<a href="# " class="easyui-linkbutton ok" onclick="savePro()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg').dialog('close')">关闭</a>
	</div>
</body>
<script>
	var currentPro;
	var status;
	var isnew = true;
	var projectitemid;
	var projectitemchildid;
	var budgetmainid;
	
	function loadData() {
		if (projectitemchildid > 0){
			$('#pro_table').datagrid({
				rownumbers : true,
				singleSelect : true,
				pagination : true,
				fitColumns : false,
				border : false,
				striped : true,
				toolbar : '#pro_tb',
				url : "budgetsubitems.do",
				method : 'get',
				queryParams : {
					projectitemchildid : projectitemchildid,
					status : status
				},
				onSelect : function(index,row) {
					currentPro = row;
				},
				onDblClickRow : function(index,row) {
					currentPro = row;
					updatepro();
				},
				columns : [ [ {
					field : 'projectitemname',
					title : '项目名称',
					width : '',
					formatter : function(value,row,index) {
						if (row.main.projectitemchild){
							return row.main.projectitemchild.name;
						}else{
							return row.main.item.name;
						}
					}
				},{
					field : 'name',
					title : '预算类别',
					width : '',
					formatter : function(value,row,index) {
						if (row.item){
							return row.item.content;
						}else{
						}
					}
				},{
					field : 'money',
					title : '金额',
					width : ''
				},{
					field : 'content',
					title : '描述',
					width : ''
				},{
					field : 'status',
					title : '状态',
					width : '',
					formatter : function(value,row,index) {
						if (value == 4){
							return value = '已审';
						}else if (value == 3){
							return value = '已拒绝';
						}else if (value == 6){
							return value = '待二级审核';
						}else if (value == 7){
							return value = '待一级审核';
						}
					}
				},{
					field : 'reason',
					title : '拒绝理由',
					width : ''
				} ] ]
			});
		}else{
			$('#pro_table').datagrid({
				rownumbers : true,
				singleSelect : true,
				pagination : true,
				fitColumns : false,
				border : false,
				striped : true,
				toolbar : '#pro_tb',
				url : "budgetsubitems.do",
				method : 'get',
				queryParams : {
					projectitemid : projectitemid,
					status : status
				},
				onSelect : function(index,row) {
					currentPro = row;
				},
				onDblClickRow : function(index,row) {
					currentPro = row;
					updatepro();
				},
				columns : [ [ {
					field : 'projectitemname',
					title : '项目名称',
					width : '',
					formatter : function(value,row,index) {
						if (row.main.projectitemchild){
							return row.main.projectitemchild.name;
						}else{
							return row.main.item.name;
						}
					}
				},{
					field : 'name',
					title : '预算类别',
					width : '',
					formatter : function(value,row,index) {
						if (row.item){
							return row.item.content;
						}else{
						}
					}
				},{
					field : 'money',
					title : '金额',
					width : ''
				},{
					field : 'content',
					title : '描述',
					width : ''
				},{
					field : 'reason',
					title : '拒绝理由',
					width : ''
				},{
					field : 'status',
					title : '状态',
					width : '',
					formatter : function(value,row,index) {
						if (value == 4){
							return value = '已审';
						}else if (value == 3){
							return value = '已拒绝';
						}else if (value == 6){
							return value = '待二级审核';
						}else if (value == 7){
							return value = '待一级审核';
						}
					}
				} ] ]
			});
		}
		if (status != 3){
			$('#pro_table').datagrid('hideColumn', 'reason');
		}
	}
	function newpro() {
		$("#proDlg").dialog('open').dialog('setTitle', '添加预算明细');
		$("#proFm").form('clear');
		currentPro = {};
		$("#budgetitem").combobox("enable");
		document.getElementById('tr1').style.display = 'none';
		document.getElementById('tr2').style.display = 'none';
		$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").show();
	}
	function updatepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			$("#proDlg").dialog('open').dialog('setTitle', '更新预算明细');
			$("#projectitem").combobox("setValue", currentPro.main.item);
			$("#projectitemchild").combobox("setValue", currentPro.main.projectitemchild);
			$("#budgetitem").combobox("setValue", currentPro.item.content);
			$("#proFm").form('load', {
				content : currentPro.content,
				money : currentPro.money
			});
			if (currentPro.status != 6){
				$("#proDlg").dialog('open').dialog('setTitle', '预算明细详细');
				$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").hide();
			}else{
				$("#proDlg").dialog('open').dialog('setTitle', '更新预算明细');
				$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").show();
			}
		}else{
			$.messager.alert('更新', '请选择要更新的预算明细', 'warning');
		}
	}
	function deletepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			if (currentPro.status == 6){
				$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
					if (data){
						$.ajax({
							type : 'POST',
							url : "budgetSubItemRemove.do?id=" + currentPro.id,
							contentType : "application/json",
							success : function(data) {
								if (data.success){
									$.messager.show({
										title : '删除',
										msg : '删除成功',
										timeout : 5000,
										showType : 'slide'
									});
									loadData();
								}else{
									$.messager.alert('删除', '删除失败', 'error');
								}
							},
							dataType : "json"
						});
					}
				});
			}else{
				$.messager.alert('警告', '已审核预算不允许删除', 'warning');
			}
		}else{
			$.messager.alert('警告', '请选择要删除的预算', 'warning');
		}
	}
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		var itemid = $("#budgetitem").combobox('getValue');
		currentPro.itemid = itemid;
		var money = $("#money").textbox('getValue');
		currentPro.money = money;
		var content = $("#content").textbox('getValue');
		currentPro.content = content;
		currentPro.mainid = budgetmainid;
		$.ajax({
			type : 'POST',
			url : "budgetSubItemSave.do",
			data : JSON.stringify(currentPro),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存预算明细',
						msg : '保存预算明细成功',
						timeout : 5000,
						showType : 'slide',
					});
					loadData();
				}else{
					$.messager.alert('保存预算明细', '保存预算明细失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function findbudgetmain(id) {
		$.ajax({
			type : 'GET',
			url : "budgetmian/searchbyprojectitemid?projectitemid=" + id,
			contentType : "application/json",
			success : function(data) {
				if (data){
					budgetmainid = data.id;
				}
			},
			dataType : "json"
		});
	}
	function findbudgetmainchild(id) {
		$.ajax({
			type : 'GET',
			url : "budgetmian/searchbyprojectitemchild?projectitemchildid=" + id,
			contentType : "application/json",
			success : function(data) {
				if (data){
					budgetmainid = data.id;
				}
			},
			dataType : "json"
		});
	}
	$.extend($.fn.textbox.defaults.rules, {
		number : {//value值为文本框中的值
			validator : function(value) {
				var reg = /^\d+(\.\d+)?$/;
				return reg.test(value);
			},
			message : "请输入数字"
		},
		chinese : {
			validator : function(value,param) {
				var reg = /^[\u4e00-\u9fa5]+$/i;
				return reg.test(value);
			},
			message : "请输入中文"
		},
		checkLength : {
			validator : function(value,param) {
				return param[0] >= get_length(value);
			},
			message : '请输入最大{0}位字符'
		},
		specialCharacter : {
			validator : function(value,param) {
				var reg = new RegExp("[`~!@#$^&*()=|{}':;'\\[\\]<>~！@#￥……&*（）——|{}【】‘；：”“'、？]");
				return !reg.test(value);
			},
			message : '不允许输入特殊字符'
		},
		end : {
			validator : function(value,param) {
				var startDate = $("#startdate").val();
				var startTmp = new Date(startDate.replace(/-/g, "/"));
				var endTmp = new Date(value.replace(/-/g, "/"));
				return startTmp <= endTmp;
			},
			message : '结束时间不能早于开始时间！'
		},
		percent : {
			validator : function(value,param) {
				var percent = $("#remainpercent").text();
				return parseFloat(value) <= parseFloat(percent);
			},
			message : '不能超过剩余百分比'
		},
		maxLength : {
			validator : function(value,param) { //value 为需要校验的输入框的值 , param为使用此规则时存入的参数
				return value.length <= param[0];
			},
			message : '不能{0}位字符.'
		}
	});
</script>

</html>